/*
 * @ClassName test_page.dart
 * @Description 测试页面
 * @Author SuPeiSen
 * @Date 2020/7/14 11:34
 */

import 'dart:io';

import 'package:dio/dio.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_su/animation/page_animation/ripple_router.dart';
import 'package:flutter_su/common/application.dart';
import 'package:flutter_su/common/fun.dart';
import 'package:flutter_su/common/global_context.dart';
import 'package:flutter_su/common/request.dart';
import 'package:flutter_su/common/show.dart';
import 'package:flutter_su/enum/language_type.dart';
import 'package:flutter_su/init/screen_util_init.dart';
import 'package:flutter_su/provider/language_provider.dart';
import 'package:flutter_su/router/test_router.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';

class TestPage extends StatefulWidget {
  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  LanguageProvider _languageProvider;

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    _languageProvider = Provider.of<LanguageProvider>(context, listen: false);
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    // 暂时在这里初始化
    GlobalContext.setAppContext(context);
    ScreenUtilInit.init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("测试页面"),
        centerTitle: true,
      ),
      body: _body(),
    );
  }

  Widget _body() {
    TextStyle style = TextStyle(
      color: Colors.blue,
    );

    return Container(
      width: double.infinity,
      child: Column(
        children: <Widget>[
          RaisedButton(
            child: Text("切换英语"),
            onPressed: () {
              _languageProvider.setLanguageType(LanguageType.english);
            },
          ),
          RaisedButton(
            child: Text("切换中文"),
            onPressed: () {
              _languageProvider.setLanguageType(LanguageType.chinese);
            },
          ),
          RaisedButton(
            child: Text("接口测试"),
            onPressed: () {
              Application.router.navigateTo(context, TestRouter.API_TEST);
            },
          ),
          RaisedButton(
            child: Text("弹窗"),
            onPressed: () {
              Application.router.navigateTo(
                context,
                TestRouter.SHOW_TEST,
                transition: TransitionType.custom,
                transitionBuilder: RippleRouteBuilder.transitionBuilder,
              );
            },
          ),
          RaisedButton(
            child: Text("路由动画"),
            onPressed: () {
              Application.router.navigateTo(context, TestRouter.ROUTER_TEST);
            },
          ),
          RaisedButton(
            child: Text("上传测试"),
            onPressed: () async {

              Show.loading();

              final ImagePicker picker = ImagePicker(); // 打开图库
              var file = await picker.getImage(source: ImageSource.gallery);
              String path = "http://gh.hellosu.top:8086/bsl_web/upload/pic";
              String token = "70988420cf8f412dbc937992c0e20908";
              var formData = {
                "file": await MultipartFile.fromFile(file.path),
                "X_Token": token,
              };

              var model = await Request().formDataRequest(path, formDataMap: formData);
              SFunCommon.debugPrint(model.toJson);
            },
          ),
        ],
      ),
    );
  }
}
